{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab qt5\n",
    "\n",
    "import os\n",
    "import numpy as np\n",
    "import glob\n",
    "import json\n",
    "\n",
    "from bgan_util import SynthDataset\n",
    "from bgan_synth import bgan_synth\n",
    "\n",
    "font = {#'family' : 'normal',\n",
    "        #'weight' : 'bold',\n",
    "        'size'   : 28}\n",
    "\n",
    "matplotlib.rc('font', **font)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10000, 100)\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'bgan_synth' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-b1f955341b10>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      6\u001b[0m \u001b[0msynth_d\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSynthDataset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_dim\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m bresults = bgan_synth(synth_d, z_dim, num_iter=1000, \n\u001b[0m\u001b[1;32m      9\u001b[0m                      \u001b[0mnumz\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnumz\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwasserstein\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m                      \u001b[0mgen_observed\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1000\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrpath\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mresults_path\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'bgan_synth' is not defined"
     ]
    }
   ],
   "source": [
    "x_dim = 100\n",
    "z_dim = 10\n",
    "numz = 10\n",
    "results_path = \"/tmp/bgan_synth\"\n",
    "\n",
    "synth_d = SynthDataset(x_dim)\n",
    "\n",
    "bresults = bgan_synth(synth_d, z_dim, num_iter=1000, \n",
    "                     numz=numz, wasserstein=False, \n",
    "                     gen_observed=1000, rpath=results_path,\n",
    "                     save_weights=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# ML DCGAN\n",
    "results = bgan_synth(synth_d, z_dim, num_iter=1000, \n",
    "                     numz=1, wasserstein=False, \n",
    "                     gen_observed=1000, rpath=results_path,\n",
    "                     save_weights=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "figure()\n",
    "plot(np.arange(100, 1001, 100), results[\"divergences\"], '--k', linewidth=2, label=\"ML GAN\")\n",
    "plot(np.arange(100, 1001, 100), bresults[\"divergences\"], '-g', linewidth=2, label=\"Bayes GAN\")\n",
    "ylim([0.0, 0.6])\n",
    "title(\"JS div. (D = %i)\" % x_dim)\n",
    "legend(loc='upper right')\n",
    "xlabel(\"No. of iterations\")\n",
    "ylabel(\"JS Divergence\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "all_wgt_files = []\n",
    "for it in range(5, 10):\n",
    "    all_wgt_files.append(\"%s/weights_%i99.npz\" % (results_dir, it))\n",
    "wgts = np.load(wgtf)\n",
    "\n",
    "wgt_vec = [list() for _ in xrange(10)]\n",
    "for name, wgt in wgts.iteritems():\n",
    "    if \"generator\" in name:\n",
    "        gi = int(name[:-2].split(\"_\")[-2])\n",
    "        wgt_vec[gi].append(wgt.flatten())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_wgt_vecs = []\n",
    "for wgtf in all_wgt_files:\n",
    "    try:\n",
    "        wgts = np.load(wgtf)\n",
    "    except:\n",
    "        continue\n",
    "    wgt_vec = [list() for _ in xrange(10)]\n",
    "    for name, wgt in wgts.iteritems():\n",
    "        if \"generator\" in name:\n",
    "            gi = int(name[:-2].split(\"_\")[-2])\n",
    "            wgt_vec[gi].append(wgt.flatten())\n",
    "\n",
    "    for gi in xrange(10):\n",
    "        wgt_v = np.concatenate(wgt_vec[gi])\n",
    "        all_wgt_vecs.append(wgt_v[:, None])\n",
    "    \n",
    "all_wgts = np.concatenate(all_wgt_vecs, axis=1)\n",
    "\n",
    "print all_wgts.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.manifold import MDS\n",
    "seed = np.random.RandomState(seed=3)\n",
    "\n",
    "mds = MDS(n_components=2, max_iter=3000, eps=1e-9, random_state=seed,\n",
    "          dissimilarity=\"euclidean\", metric=True)\n",
    "pos = mds.fit(all_wgts.T).embedding_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "figure()\n",
    "\n",
    "plot(pos[:, 0], pos[:, 1], '.')\n",
    "\n",
    "title(\"Samples from $p(\\\\theta_{g} | \\mathcal{D})$ in 2D using MDS\")\n",
    "axis(\"equal\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#results = bresults\n",
    "\n",
    "data_real = results[\"data_real\"]\n",
    "data_fake = results[\"data_fake\"][-1]\n",
    "aics_fake = results[\"all_aics_fake\"]\n",
    "\n",
    "print data_real.shape\n",
    "print type(data_fake)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "\n",
    "pca = PCA(n_components=2)\n",
    "\n",
    "data_trans_real = pca.fit_transform(data_real)\n",
    "\n",
    "print pca.explained_variance_ratio_\n",
    "\n",
    "print data_trans_real.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#pca = PCA(n_components=2)\n",
    "\n",
    "data_trans_fake = pca.transform(data_fake)\n",
    "print data_trans_fake.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "figure()\n",
    "plot(data_trans_real[:, 0], data_trans_real[:, 1], '.r')\n",
    "plot(data_trans_fake[:, 0], data_trans_fake[:, 1], '*g')"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
